package com.tianji.learning.mq;

import cn.hutool.core.util.ObjectUtil;
import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.learning.service.ILearningLessonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.tianji.common.constants.MqConstants.Exchange.ORDER_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.ORDER_PAY_KEY;
import static com.tianji.common.constants.MqConstants.Queue.LEARNING_LESSON_PAY_QUEUE;

/**
 * @param
 * @return
 */
@Slf4j
@Component
public class LisenerOrderMsg {

    @Autowired
    private ILearningLessonService iLearningLessonService;

    /**
     * 监听下单的消息
     *
     * @param orderBasicDTO
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = LEARNING_LESSON_PAY_QUEUE, durable = "true"),
            exchange = @Exchange(name = ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = ORDER_PAY_KEY
    ))
    public void lisenerMsg(OrderBasicDTO orderBasicDTO) {
        //健壮性判断
        if (ObjectUtil.isEmpty(orderBasicDTO) || ObjectUtil.isEmpty(orderBasicDTO.getUserId())
                || ObjectUtil.isEmpty(orderBasicDTO.getCourseIds())) {
            log.error("监听的消息有误，课程信息为空。");
            return;
        }
        log.info("监测到用户{}，的订单{}，将课程{}添加到课表中",
                orderBasicDTO.getUserId(), orderBasicDTO.getOrderId(), orderBasicDTO.getCourseIds());
        iLearningLessonService.coruseTime(orderBasicDTO.getUserId(), orderBasicDTO.getCourseIds());
    }

}
